/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Component;

import GameObject.Sprite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author Huy
 */
public class MyButton extends MyControlItem {

	protected MyButton() {
	}

	public MyButton(BufferedImage img, int x, int y) {

		sprite = new Sprite(img, 1, 3, x, y);
	}

	@Override
	public boolean mouseMoved(MouseEvent e) {
		if (!super.mouseMoved(e)) {
			((Sprite) sprite).setFrame(0);
			return false;
		}

		((Sprite) sprite).setFrame(1);
		return true;
	}

	@Override
	public boolean mousePressed(MouseEvent e) {
		if (!super.mousePressed(e)) {
			return false;
		}
		((Sprite) sprite).setFrame(2);
		return true;
	}

	@Override
	public boolean mouseReleased(MouseEvent e) {
		if (!super.mouseReleased(e)) {
			((Sprite) sprite).setFrame(0);
			return false;
		}
		((Sprite) sprite).setFrame(1);
		return true;
	}

	@Override
	public void draw(Graphics2D g) {
		if (!isEnable()) {
			((Sprite) sprite).setFrame(2);
		} else if (((Sprite) sprite).getFrame() == 1) {
			if (toolTipText != null) {
				Color c = g.getColor();
				Rectangle2D textBound = g.getFont().getStringBounds(toolTipText, g.getFontRenderContext());
				int x = sprite.getX();
				int y = (int) (sprite.getY() - textBound.getHeight());
				textBound.setRect(x-2, y+2, textBound.getWidth()+5, textBound.getHeight());
				//g.draw(textBound);
				g.setColor(Color.WHITE);
				g.fill(textBound);
				g.setColor(Color.BLACK);
				g.drawString(toolTipText, x, (int)(y + textBound.getHeight()));
				
				g.setColor(c);
			}
		}
		super.draw(g);
	}
}
